﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.DAL.LocalDataAccess/WaferSummaryAccess
* 类       名: WaferSummaryAccess
* 功      能:  TD
* 时      间:  2024-05-09 16:47:46
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using log4net.Util;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using SiliconSelect.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Controls.Primitives;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.Extensions;
using WaferSelectV3.Models;
using WaferSelectV3.Services;

namespace WaferSelectV3.DAL.LocalDataAccess
{
    public class WaferSummaryAccess
    {

        public static string GetTableNameByCrystalNumber(string crystalName)
        {
            try
            {
                crystalName = crystalName.Replace("[-_]", "").Trim();
                crystalName = $"RS_{crystalName}";
                return crystalName;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLogMain(ex.GetFullException(), LogType.Exception);
                return "DefaultCystalName";
            }
        }

        public static DataResult CreateWaferSummaryTable(string crystalName)
        {
            DataResult result = new DataResult();
            string tableName = GetTableNameByCrystalNumber(crystalName);
            using (var context = new DataContext())
            {
                try
                {
                    /*
                     * 1. N'[dbo].[{tableName}]'和N'U'中的N：这是SQL Server中表示Unicode字符串的前缀。在SQL Server中，
                     * Unicode字符串必须以N为前缀。如果没有N，那么字符串就会被当作非Unicode字符串处理，
                     * 这可能会导致某些Unicode字符无法正确处理。
                     *
                     * 2. type in (N'U')：这是一个判断语句，sys.objects表中的type字段代表了对象的类型。
                     * 在这里U代表用户表(用户创建的表)，因此type in (N'U')意味着在所有的用户表中查找。
                     */
                    string sql = $"IF NOT EXISTS(SELECT* FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[{tableName}]') AND type in (N'U'))" +
                                        $"BEGIN " +
                                        $"CREATE TABLE[dbo].[{tableName}](" +
                                        $"[Id] BIGINT IDENTITY(1,1) PRIMARY KEY," +
                                        $"[DetectedTime] VARCHAR(50) DEFAULT ' '," +               // 操作时间
                                        $"[ProductionDate] VARCHAR(200) DEFAULT ' '," +               // 生产日期班班次
                                        $"[WaferId] VARCHAR(50) DEFAULT ' '," +                         // 产品Id
                                        $"[CutNumber] VARCHAR(100) DEFAULT ' '," +                 // 切割编号
                                        $"[CrystalNumber] VARCHAR(100) DEFAULT ' '," +          //  晶棒编号
                                        $"[PositionNumber] VARCHAR(100) DEFAULT ' '," +         // 位置编号
                                        $"[ProductionLot] VARCHAR(100) DEFAULT ' '," +           //  生产批次
                                        $"[FlowerNumber] VARCHAR(100) DEFAULT ' '," +           // 花篮号
                                        $"[DetectionRecipe] VARCHAR(100) DEFAULT ' '," +         // 检测配方
                                        $"[DetectedLevel] VARCHAR(100) DEFAULT ' '," +            // 检测等级
                                        $"[LevelType] VARCHAR(100) DEFAULT ' '," +                   // 等级类型
                                        $"[TypeDescription] VARCHAR(100) DEFAULT ' '," +        // 类型描述
                                        $"[BoxNumber] VARCHAR(100) DEFAULT ' '," +                // 入选料盒
                                        $"[ThicknessAver] VARCHAR(100) DEFAULT ' '," +          //  平均厚度
                                        $"[TTV] VARCHAR(100) DEFAULT ' '," +                            //  TTV
                                        $"[LineTraceMax] VARCHAR(100) DEFAULT ' '," +           //  线痕最大值
                                        $"[Warping] VARCHAR(100) DEFAULT ' '," +                     //  翘曲
                                        $"[ThicknessMin] VARCHAR(100) DEFAULT ' '," +           //  最小厚度
                                        $"[ThicknessMax] VARCHAR(100) DEFAULT ' '," +          //   最大厚度
                                        $"[IsDenseLine] VARCHAR(20) DEFAULT ' '," +               //   是否密集线痕
                                        $"[LineTraceUpNum] VARCHAR(100) DEFAULT ' '," +     //   上线痕数量
                                        $"[LineTraceUpMax] VARCHAR(100) DEFAULT ' '," +      //   上线痕数量最大值
                                        $"[LineTraceDownNum] VARCHAR(100) DEFAULT ' '," +  //   下线痕数量
                                        $"[LineTraceDownMax] VARCHAR(100) DEFAULT ' '," +  //   下线痕数量最大值
                                        $"[LineTraceCount] VARCHAR(100) DEFAULT ' '," +        // 线痕数量
                                        $"[ResistivityAver] VARCHAR(100) DEFAULT ' '," +        // 电阻率
                                        $"[ResistivityMax] VARCHAR(100) DEFAULT ' '," +        // 电阻率最大值
                                        $"[ResistivityMin] VARCHAR(100) DEFAULT ' '," +        //  电阻率最小值
                                        $"[LengthMax] VARCHAR(100) DEFAULT ' '," +              //  边长最大值
                                        $"[LengthMin] VARCHAR(100) DEFAULT ' '," +              // 边长最小值
                                        $"[LengthX] VARCHAR(100) DEFAULT ' '," +                  // X边长
                                        $"[LengthY] VARCHAR(100) DEFAULT ' '," +                  // Y边长
                                        $"[DiagonalLeftStart] VARCHAR(100) DEFAULT ' '," +  // 对角线左上右下
                                        $"[DialonalRightStart] VARCHAR(100) DEFAULT ' '," + // 对角线左下右上
                                        $"[ChamferMax] VARCHAR(100) DEFAULT ' '," +          //  倒角最大值
                                        $"[ChamferMin] VARCHAR(100) DEFAULT ' '," +          // 倒角最小值
                                        $"[UpDownParallel] VARCHAR(100) DEFAULT ' '," +    // 上下平行度
                                        $"[LeftRightParallel] VARCHAR(100) DEFAULT ' '," +  // 左右平行度
                                        $"[SquareLineMax] VARCHAR(100) DEFAULT ' '," +  // 直角边最大值
                                        $"[SquareLineMin] VARCHAR(100) DEFAULT ' '," +  // 直角边最小值
                                        $"[VerticalAngleMax] VARCHAR(100) DEFAULT ' '," + // 垂直度最大值
                                        $"[VerticalAngleMin] VARCHAR(100) DEFAULT ' '," + // 垂直度最小值
                                        $"[LeftTopCharmfer] VARCHAR(100) DEFAULT ' '," +   // 左上倒角
                                        $"[RightTopCharmfer] VARCHAR(100) DEFAULT ' '," + // 右上倒角
                                        $"[LeftBottomCharmfer] VARCHAR(100) DEFAULT ' '," + // 左下倒角
                                        $"[RightBottomCharmfer] VARCHAR(100) DEFAULT ' '," + // 右下倒角
                                        $"[LeftTopAngle] VARCHAR(100) DEFAULT ' '," + // 左上角度
                                        $"[RightTopAngle] VARCHAR(100) DEFAULT ' '," + // 右上角度
                                        $"[LeftBottomAngle] VARCHAR(100) DEFAULT ' '," + // 左下角度
                                        $"[RightBottomAngle] VARCHAR(100) DEFAULT ' '," +      // 右下角度
                                        $"[LengthXMax] VARCHAR(100) DEFAULT ' '," +      // X边长最大值
                                        $"[LengthXMin] VARCHAR(100) DEFAULT ' '," +      // X边长最小值
                                        $"[LengthYMax] VARCHAR(100) DEFAULT ' '," +      // Y边长最大值
                                        $"[LengthYMin] VARCHAR(100) DEFAULT ' '," +      // Y边长最小值
                                        $"[FrontEdgeBreakExist] VARCHAR(100) DEFAULT ' '," + // 前崩边有无
                                        $"[FrontEdgeBreakNum] VARCHAR(100) DEFAULT ' '," + // 前崩边数量
                                        $"[FrontEdgeBreakArea] VARCHAR(100) DEFAULT ' '," + // 前崩边面积
                                        $"[BackEdgeBreakExist] VARCHAR(100) DEFAULT ' '," + // 后崩边有无
                                        $"[BackEdgeBreakNum] VARCHAR(100) DEFAULT ' '," + // 后崩边数量
                                        $"[BackEdgeBreakArea] VARCHAR(100) DEFAULT ' '," + // 后崩边面积
                                        $"[LeftEdgeBreakExist] VARCHAR(100) DEFAULT ' '," + // 左崩边有无
                                        $"[LeftEdgeBreakNum] VARCHAR(100) DEFAULT ' '," + // 左崩边数量
                                        $"[LeftSurfaceBreakExist] VARCHAR(100) DEFAULT ' '," + // 左表崩有无
                                        $"[LeftSurfaceBreakNum] VARCHAR(100) DEFAULT ' '," + // 左表崩数量
                                        $"[RightEdgeBreakExist] VARCHAR(100) DEFAULT ' '," + // 右崩边有无
                                        $"[RightEdgeBreakNum] VARCHAR(100) DEFAULT ' '," + // 右崩边数量
                                        $"[RightSurfaceBreakExist] VARCHAR(100) DEFAULT ' '," + // 右表崩有无
                                        $"[RightSurfaceBreakNum] VARCHAR(100) DEFAULT ' '," + // 右表崩数量
                                        $"[SumdgeUpExist] VARCHAR(100) DEFAULT ' '," + // 上脏污有无
                                        $"[SmudgeUpArea] VARCHAR(100) DEFAULT ' '," + // 上脏污面积
                                        $"[SumdgeUpNum] VARCHAR(100) DEFAULT ' '," + // 上脏污数量
                                        $"[SumdgeUpReverse] VARCHAR(100) DEFAULT ' '," + // 上脏污是否反向
                                        $"[SumdgeUpHoleExist] VARCHAR(100) DEFAULT ' '," + // 上脏污穿孔有无
                                        $"[SumdgeUpHoleNum] VARCHAR(100) DEFAULT ' '," + // 上脏污穿孔数量
                                        $"[HoleExistUp] VARCHAR(100) DEFAULT ' '," + // 上脏污缺口
                                        $"[HoleNumUp] VARCHAR(100) DEFAULT ' '," + // 上脏污缺口数量
                                         $"[SumdgeDownExist] VARCHAR(100) DEFAULT ' '," + // 下脏污有无
                                        $"[SumdgeDownArea] VARCHAR(100) DEFAULT ' '," + // 下脏污面积
                                        $"[SumdgeDownNum] VARCHAR(100) DEFAULT ' '," + // 下脏污数量
                                        $"[SumdgeDownReverse] VARCHAR(100) DEFAULT ' '," + // 下脏污是否反向
                                        $"[SumdgeDownHoleExist] VARCHAR(100) DEFAULT ' '," + // 下脏污穿孔有无
                                        $"[SumdgeDownHoleNum] VARCHAR(100) DEFAULT ' '," + // 下脏污穿孔数量
                                        $"[HoleExistDown] VARCHAR(100) DEFAULT ' '," + // 下脏污缺口有无
                                        $"[HoleNumDown] VARCHAR(100) DEFAULT ' '," + // 下脏污孔洞数量
                                        $"[SubfissureExist01] VARCHAR(100) DEFAULT ' '," + // 隐裂1_隐裂有无
                                        $"[SubfissureArea01] VARCHAR(100) DEFAULT ' '," + // 隐裂1_隐裂面积
                                        $"[SubfissureNum01] VARCHAR(100) DEFAULT ' '," + // 隐裂1_隐裂数量
                                        $"[SubfissureBreakExist01] VARCHAR(100) DEFAULT ' '," + // 隐裂1_崩边有无
                                        $"[SubfissureBreakArea01] VARCHAR(100) DEFAULT ' '," + // 隐裂1_崩边面积
                                        $"[SubfissureBreakNum01] VARCHAR(100) DEFAULT ' '," + // 隐裂1_崩边数量
                                        $"[SubfissureHoleExist01] VARCHAR(100) DEFAULT ' '," + // 隐裂1_孔洞有无
                                        $"[SubfissureHoleNum01] VARCHAR(100) DEFAULT ' '," + // 隐裂1_孔洞数量
                                        $"[SubfissureExist02] VARCHAR(100) DEFAULT ' '," + // 隐裂2_隐裂有无
                                        $"[SubfissureArea02] VARCHAR(100) DEFAULT ' '," + // 隐裂2_隐裂面积
                                        $"[SubfissureNum02] VARCHAR(100) DEFAULT ' '," + // 隐裂2_隐裂数量
                                        $"[SubfissureBreakExist02] VARCHAR(100) DEFAULT ' '," + // 隐裂2_崩边有无
                                        $"[SubfissureBreakArea02] VARCHAR(100) DEFAULT ' '," + // 隐裂2_崩边面积
                                        $"[SubfissureBreakNum02] VARCHAR(100) DEFAULT ' '," + // 隐裂2_崩边数量
                                        $"[SubfissureHoleExist02] VARCHAR(100) DEFAULT ' '," + // 隐裂2_孔洞有无
                                        $"[SubfissureHoleNum02] VARCHAR(100) DEFAULT ' ');" + // 隐裂2_孔洞数量
                                        $" END";
                    context.Database.ExecuteSqlRaw(sql);
                    // 如果数据表为空,就插入一条数据(admin hdno1管理员)
                    sql = $"Select * from [{tableName}]";
                    int count = context.WaferSummaries.FromSqlRaw(sql).ToList().Count;
                    if (count <= 0)
                    {
                        string infoMessage = $"数据表{tableName} 创建成功";
                        LoggerHelper.WriteLogMain(infoMessage, LogType.Info);
                        sql = $"INSERT INTO {tableName} (DetectedTime,ProductionDate, WaferId, CutNumber, CrystalNumber, PositionNumber, ProductionLot, FlowerNumber, DetectionRecipe, DetectedLevel, LevelType, " +
                            $"TypeDescription, BoxNumber, ThicknessAver, TTV, LineTraceMax, Warping, ThicknessMin, ThicknessMax, IsDenseLine, LineTraceUpNum, LineTraceUpMax," +
                            $" LineTraceDownNum, LineTraceDownMax, LineTraceCount, ResistivityAver, ResistivityMax, ResistivityMin, LengthMax, LengthMin, LengthX, LengthY, DiagonalLeftStart, " +
                            $"DialonalRightStart, ChamferMax, ChamferMin, UpDownParallel, LeftRightParallel, SquareLineMax, SquareLineMin, VerticalAngleMax, VerticalAngleMin, LeftTopCharmfer, " +
                            $"RightTopCharmfer, LeftBottomCharmfer, RightBottomCharmfer, LeftTopAngle, RightTopAngle, LeftBottomAngle, RightBottomAngle,LengthXMax,LengthXMin,LengthYMax,LengthYMin," +
                            $"FrontEdgeBreakExist, FrontEdgeBreakNum, FrontEdgeBreakArea," +
                            $"BackEdgeBreakExist, BackEdgeBreakNum, BackEdgeBreakArea,LeftEdgeBreakExist, LeftEdgeBreakNum, LeftSurfaceBreakNum, LeftSurfaceBreakExist, RightEdgeBreakExist, " +
                            $"RightEdgeBreakNum, RightSurfaceBreakNum, " +
                            $"RightSurfaceBreakExist,SumdgeUpExist,SmudgeUpArea,SumdgeUpNum,SumdgeUpReverse,SumdgeUpHoleExist,SumdgeUpHoleNum,HoleExistUp,HoleNumUp,SumdgeDownExist," +
                            $"SumdgeDownArea,SumdgeDownNum,SumdgeDownReverse,SumdgeDownHoleExist,SumdgeDownHoleNum,HoleExistDown,HoleNumDown, SubfissureArea01, " +
                            $"SubfissureNum01, SubfissureExist01, SubfissureBreakArea01, SubfissureBreakNum01, SubfissureBreakExist01, SubfissureHoleNum01, SubfissureHoleExist01, SubfissureArea02, " +
                            $"SubfissureNum02, SubfissureExist02, SubfissureBreakArea02, SubfissureBreakNum02, SubfissureBreakExist02, SubfissureHoleNum02, SubfissureHoleExist02) " +
                            $"VALUES (@DetectedTime,@ProductionDate, @WaferId, @CutNumber, @CrystalNumber, @PositionNumber, @ProductionLot, @FlowerNumber, @DetectionRecipe, @DetectedLevel, @LevelType, " +
                            $"@TypeDescription, @BoxNumber, @ThicknessAver, @TTV, @LineTraceMax, @Warping, @ThicknessMin, @ThicknessMax, @IsDenseLine, @LineTraceUpNum, @LineTraceUpMax, " +
                            $"@LineTraceDownNum, @LineTraceDownMax, @LineTraceCount, @ResistivityAver, @ResistivityMax, @ResistivityMin, @LengthMax, @LengthMin, @LengthX, @LengthY, @DiagonalLeftStart, " +
                            $"@DialonalRightStart, @ChamferMax, @ChamferMin, @UpDownParallel, @LeftRightParallel, @SquareLineMax, @SquareLineMin, @VerticalAngleMax, @VerticalAngleMin, @LeftTopCharmfer, " +
                            $"@RightTopCharmfer, @LeftBottomCharmfer, @RightBottomCharmfer, @LeftTopAngle, @RightTopAngle, @LeftBottomAngle, @RightBottomAngle,@LengthXMax,@LengthXMin,@LengthYMax,@LengthYMin," +
                            $" @FrontEdgeBreakExist, @FrontEdgeBreakNum, @FrontEdgeBreakArea," +
                            $"@BackEdgeBreakExist, @BackEdgeBreakNum,@BackEdgeBreakArea, @LeftEdgeBreakExist, @LeftEdgeBreakNum, @LeftSurfaceBreakNum, @LeftSurfaceBreakExist, @RightEdgeBreakExist," +
                            $" @RightEdgeBreakNum, @RightSurfaceBreakNum," +
                            $"@RightSurfaceBreakExist, " +
                            $"@SumdgeUpExist,@SmudgeUpArea,@SumdgeUpNum,@SumdgeUpReverse,@SumdgeUpHoleExist,@SumdgeUpHoleNum,@HoleExistUp,@HoleNumUp," +
                            $"@SumdgeDownExist,@SumdgeDownArea,@SumdgeDownNum,@SumdgeDownReverse,@SumdgeDownHoleExist,@SumdgeDownHoleNum,@HoleExistDown,@HoleNumDown," +
                            $" @SubfissureArea01, @SubfissureNum01, " +
                            $"@SubfissureExist01, @SubfissureBreakArea01, @SubfissureBreakNum01, @SubfissureBreakExist01, @SubfissureHoleNum01, @SubfissureHoleExist01, @SubfissureArea02, @SubfissureNum02, " +
                            $"@SubfissureExist02, @SubfissureBreakArea02, @SubfissureBreakNum02, @SubfissureBreakExist02, @SubfissureHoleNum02, @SubfissureHoleExist02);";

                        int waferIdStart = 1;
                        int insertCount = 8000;
                        for (int i = 0; i < insertCount; i++)
                        {
                            SqlParameter[] parameters = new SqlParameter[]
                            {
                                new SqlParameter("@DetectedTime", SqlDbType.VarChar) { Value = DateTimeHelper.GetTimeNowForSql() },
                                new SqlParameter("@ProductionDate",SqlDbType.VarChar){Value = DateTimeHelper.GetProductionDateTime()},
                                new SqlParameter("@WaferId", SqlDbType.VarChar) { Value = (waferIdStart+i).ToString() },
                                new SqlParameter("@CutNumber", SqlDbType.VarChar) { Value = "TestCrystal007"},
                                new SqlParameter("@CrystalNumber", SqlDbType.VarChar) { Value = "TestCrystal007" },
                                new SqlParameter("@PositionNumber", SqlDbType.VarChar) { Value =i.ToString()},
                                new SqlParameter("@ProductionLot", SqlDbType.VarChar) { Value = "123"},
                                new SqlParameter("@FlowerNumber", SqlDbType.VarChar) { Value = "123" },
                                new SqlParameter("@DetectionRecipe", SqlDbType.VarChar) { Value =PathDirProvider.ProductionRecipeName },
                                new SqlParameter("@DetectedLevel", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LevelType", SqlDbType.VarChar) { Value = $"{i}级" },
                                new SqlParameter("@TypeDescription", SqlDbType.VarChar) { Value = $"等级{i}描述" },
                                new SqlParameter("@BoxNumber", SqlDbType.VarChar) { Value = $"{i+100}" },
                                new SqlParameter("@ThicknessAver", SqlDbType.VarChar) { Value = (i+30).ToString()},
                                new SqlParameter("@TTV", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LineTraceMax", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@Warping", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@ThicknessMin", SqlDbType.VarChar) { Value = $"{i+10}" },
                                new SqlParameter("@ThicknessMax", SqlDbType.VarChar) { Value = (i+30).ToString()},
                                new SqlParameter("@IsDenseLine", SqlDbType.VarChar) { Value = "0" },
                                new SqlParameter("@LineTraceUpNum", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LineTraceUpMax", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LineTraceDownNum", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LineTraceDownMax", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LineTraceCount", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@ResistivityAver", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@ResistivityMax", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@ResistivityMin", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LengthMax", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LengthMin", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LengthX", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LengthY", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@DiagonalLeftStart", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@DialonalRightStart", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@ChamferMax", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@ChamferMin", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@UpDownParallel", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LeftRightParallel", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SquareLineMax", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SquareLineMin", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@VerticalAngleMax", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@VerticalAngleMin", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LeftTopCharmfer", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@RightTopCharmfer", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LeftBottomCharmfer", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@RightBottomCharmfer", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LeftTopAngle", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@RightTopAngle", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LeftBottomAngle", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@RightBottomAngle", SqlDbType.VarChar) { Value = i.ToString() },

                                new SqlParameter("@LengthXMax", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LengthXMin", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LengthYMax", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LengthYMin", SqlDbType.VarChar) { Value = i.ToString() },

                                new SqlParameter("@FrontEdgeBreakExist", SqlDbType.VarChar) { Value ="0"  },
                                new SqlParameter("@FrontEdgeBreakNum", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@FrontEdgeBreakArea", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@BackEdgeBreakExist", SqlDbType.VarChar) { Value = "0"},
                                new SqlParameter("@BackEdgeBreakNum", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@BackEdgeBreakArea", SqlDbType.VarChar) { Value = i.ToString() },

                                new SqlParameter("@LeftEdgeBreakNum", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LeftEdgeBreakExist", SqlDbType.VarChar) { Value = "0" },
                                new SqlParameter("@LeftSurfaceBreakNum", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@LeftSurfaceBreakExist", SqlDbType.VarChar) { Value = "0"},
                                new SqlParameter("@RightEdgeBreakNum", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@RightEdgeBreakExist", SqlDbType.VarChar) { Value = "0" },
                                new SqlParameter("@RightSurfaceBreakNum", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@RightSurfaceBreakExist", SqlDbType.VarChar) { Value = "0" },
                                new SqlParameter("@SumdgeUpExist", SqlDbType.VarChar) { Value = "0" },
                                new SqlParameter("@SmudgeUpArea", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SumdgeUpNum", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SumdgeUpReverse",SqlDbType.VarChar){Value = "0" },
                                new SqlParameter("@SumdgeUpHoleExist", SqlDbType.VarChar) { Value = "0" },
                                new SqlParameter("@SumdgeUpHoleNum", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@HoleExistUp", SqlDbType.VarChar) { Value = "0" },
                                new SqlParameter("@HoleNumUp", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SumdgeDownExist", SqlDbType.VarChar) { Value ="0" },
                                new SqlParameter("@SumdgeDownArea", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SumdgeDownNum", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SumdgeDownReverse",SqlDbType.VarChar){Value = "0"},
                                new SqlParameter("@SumdgeDownHoleExist", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SumdgeDownHoleNum", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@HoleExistDown", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@HoleNumDown", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SubfissureArea01", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SubfissureNum01", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SubfissureExist01", SqlDbType.VarChar) { Value = "0" },
                                new SqlParameter("@SubfissureBreakArea01", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SubfissureBreakNum01", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SubfissureBreakExist01", SqlDbType.VarChar) { Value = "0" },
                                new SqlParameter("@SubfissureHoleNum01", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SubfissureHoleExist01", SqlDbType.VarChar) { Value = "0" },
                                new SqlParameter("@SubfissureArea02", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SubfissureNum02", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SubfissureExist02", SqlDbType.VarChar) { Value = "0" },
                                new SqlParameter("@SubfissureBreakArea02", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SubfissureBreakNum02", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SubfissureBreakExist02", SqlDbType.VarChar) { Value = "0" },
                                new SqlParameter("@SubfissureHoleNum02", SqlDbType.VarChar) { Value = i.ToString() },
                                new SqlParameter("@SubfissureHoleExist02", SqlDbType.VarChar) { Value = i.ToString() },
                            };
                            //这里插入数据是用来测试,正常生产部需要插入数据.
                            int ret = context.Database.ExecuteSqlRaw(sql, parameters);
                            if (ret <= 0)
                            {
                                infoMessage = $"数据表{tableName} 插入数据失败!";
                                throw new Exception(infoMessage);
                            }
                        }
                        if (insertCount > 1)
                        {
                            infoMessage = $"数据表{tableName} 插入{insertCount}条数据成功数据表成功!";
                            LoggerHelper.WriteLogMain(infoMessage, LogType.Test);
                        }

                    }
                    else
                    {
                        LoggerHelper.WriteLogMain($"{tableName} 数据表已经存在,并且至少有一条数据", LogType.Test);
                    }
                    result.State = true;
                }
                catch (Exception ex)
                {
                    result.Message = ex.GetFullException();
                }
                return result;
            }
        }


        /// <summary>
        /// 默认按照流水号的倒叙排列
        /// </summary>
        /// <param name="crystalNumber"></param>
        /// <param name="isDesc"></param>
        /// <returns></returns>
        public static DataResult<List<WaferSummaryEntity>> GetResultSummaryByCrystalName(string crystalNumber, bool isDesc = false)
        {
            DataResult<List<WaferSummaryEntity>> result = new DataResult<List<WaferSummaryEntity>>();
            try
            {
                string tableName = GetTableNameByCrystalNumber(crystalNumber);
                string direction = isDesc ? "Desc" : "Asc";
                string sql = $"select * from {tableName} Order By WaferId + 1 {direction}";

                using (var dataContext = new DataContext())
                {
                    List<WaferSummaryEntity> dataFinded = dataContext.WaferSummaries.FromSqlRaw(sql).ToList();
                    result.Data = dataFinded;
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }

            return result;
        }

        /// <summary>
        /// 获取所有的晶棒信息的表名
        /// </summary>
        /// <returns></returns>
        public static DataResult<List<string>> GetTotalResultSummaryTableNames()
        {
            DataResult<List<string>> result = new DataResult<List<string>>();
            List<string> tableNames = new List<string>();
            try
            {
                using (var dataContext = new DataContext())
                {
                    var command = dataContext.Database.GetDbConnection().CreateCommand();
                    string sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'";
                    command.CommandText = sql;
                    dataContext.Database.OpenConnection();
                    using (var readRes = command.ExecuteReader())
                    {
                        while (readRes.Read())
                        {
                            tableNames.Add(readRes.GetString(0));
                        }
                    }
                    tableNames = tableNames.Where(x => x.StartsWith("RS_")).ToList();
                    result.Data = tableNames;
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 将一个waferModel列表保存到Csv文件中
        /// </summary>
        /// <param name="savePath"></param>
        /// <param name="resultSummaryTotal"></param>
        /// <param name="isAppend"></param>
        /// <returns></returns>
        public static DataResult SaveSummaryListToCsv(string savePath, List<WaferSummaryModel>? resultSummaryTotal, bool isAppend)
        {
            DataResult result = new DataResult();
            try
            {
                List<List<string>> saveData = GetListString(resultSummaryTotal);
                FileOperateHelper.CreateAnEmptyFile(savePath);
                saveData.Insert(0, ConstantProvider.SummaryTitle);
                CsvHelper.WriteToCsvMultiLines(savePath, saveData, isAppend);
                File.SetAttributes(savePath, FileAttributes.Normal);
                result.State = true;
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        private static List<List<string>> GetListString(List<WaferSummaryModel>? resultSummaryTotal)
        {
            List<List<string>> result = new List<List<string>>();
            foreach (var model in resultSummaryTotal!)
            {
                List<string?> lineStr = new List<string?>()
                {
                    model.DetectedTime,
                    model.ProductionDate,
                    model.WaferId,
                    model.CutNumber,
                    model.CrystalNumber,
                    model.PositionNumber,
                    model.ProductionLot,
                    model.FlowerNumber,
                    model.DetectionRecipe,
                    model.DetectedLevel,
                    model.LevelType,
                    model.TypeDescription,
                    model.BoxNumber,
                    model.ThicknessAver,
                    model.TTV,
                    model.LineTraceMax,
                    model.Warping,
                    model.ResistivityAver,
                    model.ResistivityMax,
                    model.ResistivityMin,
                    model.LengthXMax,
                    model.LengthXMin,
                    model.LengthYMax,
                    model.LengthYMin,
                    model.LengthMax,
                    model.LengthMin,
                    model.LengthX,
                    model.LengthY,
                    model.DiagonalLeftStart,
                    model.DialonalRightStart,
                    model.ChamferMax,
                    model.ChamferMin,
                    model.UpDownParallel,
                    model.LeftRightParallel,
                    model.SquareLineMax,
                    model.SquareLineMin,
                    model.VerticalAngleMax,
                    model.VerticalAngleMin,
                    model.LeftTopCharmfer,
                    model.RightTopCharmfer,
                    model.LeftBottomCharmfer,
                    model.RightBottomCharmfer,
                    model.LeftTopAngle,
                    model.RightTopAngle,
                    model.LeftBottomAngle,
                    model.RightBottomAngle,
                    model.FrontEdgeBreakExist,
                    model.FrontEdgeBreakNum,
                    model.FrontEdgeBreakArea,
                    model.BackEdgeBreakExist,
                    model.BackEdgeBreakNum,
                    model.BackEdgeBreakArea,
                    model.LeftEdgeBreakExist,
                    model.LeftEdgeBreakNum,
                    model.LeftSurfaceBreakExist,
                    model.LeftSurfaceBreakNum,
                    model.RightEdgeBreakExist,
                    model.RightEdgeBreakNum,
                    model.RightSurfaceBreakExist,
                    model.RightSurfaceBreakNum,
                    model.ThicknessMin,
                    model.ThicknessMax,
                    model.IsDenseLine,
                    model.LineTraceUpNum,
                    model.LineTraceUpMax,
                    model.LineTraceDownNum,
                    model.LineTraceDownMax,
                    model.LineTraceCount,
                    model.SumdgeUpExist,
                    model.SmudgeUpArea,
                    model.SumdgeUpNum,
                    model.SumdgeUpReverse,
                    model.HoleExistUp,
                    model.SumdgeUpHoleExist,
                    model.SumdgeDownExist,
                    model.SumdgeDownArea,
                    model.SumdgeDownNum,
                    model.SumdgeDownReverse,
                    model.HoleExistDown,
                    model.SumdgeDownHoleExist,
                    model.SubfissureExist01,
                    model.SubfissureArea01,
                    model.SubfissureNum01,
                    model.SubfissureBreakExist01,
                    model.SubfissureBreakArea01,
                    model.SubfissureBreakNum01,
                    model.SubfissureHoleExist01,
                    model.SubfissureHoleNum01,
                   model.SubfissureExist02,
                    model.SubfissureArea02,
                    model.SubfissureNum02,
                    model.SubfissureBreakExist02,
                    model.SubfissureBreakArea02,
                    model.SubfissureBreakNum02,
                    model.SubfissureHoleExist02,
                    model.SubfissureHoleNum02,
                };
                result.Add(lineStr!);
            }
            return result;
        }
    }
}
